#include <vector>
#include "iostream"

using namespace std;

class Solution {
public:
    int totalFruit(vector<int> &fruits) {
        int cnt[fruits.size() + 1];
        for (int i = 0; i <= fruits.size(); ++i) {
            cnt[i] = 0;
        }
        int num = 0, i = 0, j = 0, res = 0;
        for (; j < fruits.size(); ++j) {
            if (!cnt[fruits[j]]) num++;
            cnt[fruits[j]]++;
            while (num > 2) {
                if (cnt[fruits[i]] == 1) {
                    num--;
                }
                cnt[fruits[i]]--;
                i++;
            }
            res = max(res, j - i + 1);
        }
        return res;
    }
};

int main() {
    vector<int> fruits = {3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4};
    Solution sl;
    printf("%d", sl.totalFruit(fruits));
    return 0;
}